//
//  HTRefreshControl.swift
//  WeiBoDemo
//
//  Created by 郝涛 on 16/4/22.
//  Copyright © 2016年 郝涛. All rights reserved.
//

import UIKit
enum HTRefreshControlType:Int{
    case Normal = 0 //正常转台
    case Pulling = 1 //松手刷新
    case Refreshint = 2 //刷新中
}

class HTRefreshControl: UIControl {
    
    var HTState:HTRefreshControlType = .Normal{
        didSet{
            switch HTState{
            case .Normal:
                print("正常状态")
                messageLabel.text = "正常状态"
                // 我们上一个状态为Refreshing 在执行动画
                if oldValue == .Refreshint{
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        self.scrollView?.contentInset.top -= 50
                    })
                }
            case .Pulling:
                print("松手刷新")
                messageLabel.text = "松手刷新"
            case .Refreshint:
                print("刷新中")
                messageLabel.text = "刷新中"
            
                // 设置动画 增加contentInset.top  50的高度
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.scrollView?.contentInset.top += 50
                    }, completion: { (_) -> Void in
                        // 告知外界要刷新了
                        self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
                })
            }
     
        }
        
    }
    var scrollView:UIScrollView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func endRefreshing(){
        
        HTState = .Normal
    }
    //设置视图
    private func setupUI(){
        self.backgroundColor = UIColor.orangeColor()
       //添加label
        addSubview(messageLabel)
        
    }
    
    //设置控件的约束
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置frame
        self.frame = CGRect(x: 0, y: -50, width: SCREENW, height: 50)
//        messageLabel的frame
        messageLabel.frame = CGRect(x: 0, y: 0, width: SCREENW, height: 50)
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        print(newSuperview)
        
        //判断父控件是否存在  且是否是UIScrollView
        if let scrollView = newSuperview as?UIScrollView{
            //kvo监听我们父控件的属性改变
            self.scrollView = scrollView
            scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
            
            
//            print("可以滑动")
        }

    }
        //移除kvo
    deinit{
    
        self.scrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }
 //监听对象属性发生改变
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
       let contentOffsetY = scrollView?.contentOffset.y
        //拖动中
        if self.scrollView!.dragging{
        
        if contentOffsetY > -114 && HTState == .Pulling {
           
            HTState = .Normal
        }else if contentOffsetY <= -114 && HTState == .Normal{
           
            HTState = .Pulling
        }
        }else{//停止拖动了
            if HTState == .Pulling{
                //进入刷新中
                HTState = .Refreshint
            }
            
        }
    }
    
    //懒加载文字label
    private lazy var messageLabel :UILabel = {
        let leb = UILabel()
        leb.font = UIFont.systemFontOfSize(14)
        leb.text = "正常状态"
        leb.textAlignment = .Center
        leb.textColor = UIColor.whiteColor()
        return leb
    }()
}
